<?php
	include_once 'isLogin.php';
	
	$table_name = "xinjian";
	
	if($_POST['submit']){
		//编辑状态先获得原内容
		if($_GET['id']!=false){
			$oldImg=$commonc->getInfo($table_name,$_GET['id'],'img,file');
			$img=$oldImg['img'];
			$file = $oldImg['file'];
		}
		
		//上传图片，生成缩略图
		if($_FILES['img']['name']!=''){
			//设置路径
			$folder = "../upload/test/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img);
			@unlink($middleFolder.$img);
			@unlink($smallFolder.$img);
			//上传新图
			$upimg = new uploadImg();
			$img = $upimg->upLoad('img',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img,$smallFolder);
		}
		
		//上传文件
		if($_FILES['file']['name']!=''){
			@unlink('../upload/test/file/'.$file);
			$file = $commonc->uploadFile('file','../upload/test/file/');
		}
		
		//插入数据库
		$arr = array('title'=>$_POST['title'],'content'=>$_POST['content'],'img'=>$img,'file'=>$file);
		if($_GET['id']!=false){
			$commonc->update($table_name,$arr,'id='.$_GET['id']);
			$href='common_add.php?id='.$_GET['id'];
		}else{
			$commonc->insert($table_name,$arr);
			$href='common_add.php';
			$id = $commonc->insert_id();
			$arr = array('sort'=>$id);
			$commonc->update($table_name, $arr, "id='".$id."'");
		}
		echo $commonc->submitAlert('成功',$href);
	}
	
	//获得数据
	if($_GET['id']!=false){
		$row=$commonc->getInfo($table_name,$_GET['id']);
		$smarty->assign('row',$row);
	}
	
	$smarty->display("common_add1.html");
?>